<?php
class Wxapi{
    protected $AppID = 'wx8257ce1de92e99b4';
	protected $AppSecret = 'd4624c36b6795d1d99dcf0547af5443d';
	
 /*   public function __construct($db, $data) {
		$this->db = $db;
    }
	*/
	public function wx_oauth(){		
		//$REDIRECT_URI = SITE_ADDR.'default/wxlogin.htm';
		$REDIRECT_URI = SITE_ADDR.'default/wxlogin.htm';		
		$state = random_code(20);		
		$url = 'https://open.weixin.qq.com/connect/qrconnect?appid='.$this->AppID.'&redirect_uri='.$REDIRECT_URI.'&response_type=code&scope=snsapi_login&state='.$state.'#wechat_redirect';
		$_SESSION['status'] = $status;
		header("Location:".$url);
		exit;			
	}
	
	public function get_access_token($code){
		$url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$this->AppID.'&secret='.$this->AppSecret.'&code='.$code.'&grant_type=authorization_code';
		$cfg['ssl'] = true;
		$result = curlOpen($url,$cfg);				
		return json_decode($result,true);			
	}
	
	public function get_user_info($code){
		$access_token = $this->get_access_token($code);
		$cfg['ssl'] = true;
		$url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token['access_token'].'&openid='.$access_token['openid'];
		$result = curlOpen($url,$cfg);
		return json_decode($result,true);	
		
	}

   

}

?>